package cn.pconline.autoclub.service;

import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.ClubBrand;

/**
 * 车友会品牌service
 * @author zhoutianhuai
 *
 * 2015年2月10日 下午2:57:41
 */
public class ClubBrandService {
	  @Autowired
	  private GeliDao geliDao;
	  @Autowired
	  private GeliOrm geliOrm;
	  
	  /**
	   * 更新车友会品牌，数据库已经有就更新（避免重复）
	   * @param brand
	   */
	  public void updateClubBrand(ClubBrand brand){
		  if(brand == null){
			  return;
		  }
		  SqlBuilder sqlBuilder = new SqlBuilder();
		  sqlBuilder.appendSql(" insert into ").appendSql(geliOrm.getTableName(ClubBrand.class));
		  sqlBuilder.appendSql(" (brand_id,brand_name,initial,create_at)  values(?,?,?,now()) ON DUPLICATE KEY UPDATE update_at = now()");
		  Object[] objects = new Object[]{ brand.getBrandId(), brand.getBrandName(), brand.getInitial() };
	      geliDao.getJdbcTemplate().update(sqlBuilder.getSql(), objects);
	      
	      //清除mc
	      geliDao.deleteCache(ClubBrand.class, brand.getBrandId());
	      
	  }
	  
	  /**
	   * 查询所有车友会品牌
	   * @return
	   */
	  public List<ClubBrand> getAllClubBrand(){
		  SqlBuilder sqlBuilder = new SqlBuilder();
		  sqlBuilder.appendSql(" select brand_id from ").appendSql(geliOrm.getTableName(ClubBrand.class));
		  sqlBuilder.appendSql(" order by initial");
		  return geliDao.list(ClubBrand.class, sqlBuilder.getSql(), sqlBuilder.getValues());
	  }
	  
}
